% Chapter 2 ML estimation
% VAR, Loss Distribution, and SVD metrics calculation

clc
clear all


%% To compare VaR and loss distribution of cohort, duration, ml
load('D:\My Documents\CIRANO\db_mySQL\simulation_yr12_10k_ml_CreditMetric.mat','lossGrp')
lossGrpMl = lossGrp; 
clear lossGrp;
load('D:\My Documents\CIRANO\db_mySQL\simulation_yr12_10k_cohort_CreditMetric.mat','lossGrp','SG')
lossGrpCoh = lossGrp;
clear lossGrp;
load('D:\My Documents\CIRANO\db_mySQL\simulation_yr12_10k_duration_CreditMetric_case2.mat','lossGrp')
lossGrpDur = lossGrp;
clear lossGrp;

% VaR as distance of quantile and mean.
VaRGrpCoh = quantile (lossGrpCoh,[.99 .999]);%-repmat(MiuCoh,2,1);
VaRGrpDur = quantile (lossGrpDur,[.99 .999]);%-repmat(MiuDur,2,1);
VaRGrpMl = quantile (lossGrpMl,[.99 .999]);%- repmat(MiuMl,2,1);



MiuGrpCoh = mean(lossGrpCoh);
SigGrpCoh = std(lossGrpCoh,0,1);
MiuGrpDur = mean(lossGrpDur);
SigGrpDur = std(lossGrpDur,0,1);
MiuGrpMl = mean(lossGrpMl);
SigGrpMl = std(lossGrpMl,0,1);

% for grp = 1:4
% subplot(2,2,grp);
% colormap(summer)
% b(:,1) = lossGrpCoh(:,grp);
% b(:,2) = lossGrpDur(:,grp);
% b(:,3) = lossGrpMl(:,grp);
% hist(b,35);
% title(SG(grp));
% xlabel('Loss');
% ylabel('Frequency')  
% set(gca, 'Ylim',[0, 1800]);
% legend('Cohort','Duration','ML')
% grid on
% end

%% To compare SVD
load('D:\My Documents\CIRANO\db_mySQL\tp_2011May29_MR7_WR_case2.mat','cohTpGrpStd','durTpGrpStd','conditions','numEdgesStd','numGrp','NUMBER_OF_YEAR');
load('D:\My Documents\CIRANO\db_mySQL\mlTpGrpStd.mat');
year_list=(1998:2009)';
year_list_text=num2str(year_list);
SVD = zeros(NUMBER_OF_YEAR,numGrp, 3); % columns represent coh, dur and ml results
deltaSVD1 = zeros(NUMBER_OF_YEAR,numGrp,1);
deltaSVD2 = zeros(NUMBER_OF_YEAR,numGrp,1);


% SVD
for yearIdx = 1:NUMBER_OF_YEAR
for cdIdx =1:length(conditions)
    
coh (:,:) =  cohTpGrpStd(cdIdx, yearIdx, :,:)./100;
dur (:,:) =  durTpGrpStd(cdIdx, yearIdx, :,:)./100;
ml (:,:) = mlTpGrpStd(cdIdx,yearIdx,:,:);

SVD(yearIdx, cdIdx,1) = mean(svd(coh-eye(numEdgesStd)));
SVD(yearIdx, cdIdx,2) = mean(svd(dur-eye(numEdgesStd)));
SVD(yearIdx, cdIdx,3) = mean(svd(ml-eye(numEdgesStd)));
% avgCoh(:,:) = avgCohTpGrpStd(cdIdx,:,:)./100;

deltaSVD1(yearIdx,cdIdx,1) = SVD(yearIdx, cdIdx,1) - SVD(yearIdx, cdIdx,2);
deltaSVD2(yearIdx,cdIdx,1) = SVD(yearIdx, cdIdx,3) - SVD(yearIdx, cdIdx,2);
end
end


%plot (deltaSVD1(:,:,1));
Y = cellstr(char('1998','1999','2000','2001','2002','2003','2004','2005','2006','2007','2008','2009'));
R = cellstr(char('1.5','2','2.5','3','3.5','4','4.5'));
SG = cellstr(char('Total Sample: [$50k,+)','Sample A: [$50k, 250k)','Sample B: [$250k, 1000k)','Sample C: [$1000k, +)'));


set(0,'DefaultAxesColorOrder',[0 0 1],...
      'DefaultAxesLineStyleOrder','-|--|:|-.')

figure ; 
for i = 1:length(conditions)
subplot (2, 2, i);
x(:,:) = SVD(:,i,:);
plot(x,'LineWidth',1.3)
xlabel('Years');
ylabel('SVD metric');
set(gca, 'Xlim',[1, 12]);
set(gca,'XTick',[1:1:12]);
set(gca, 'Ylim',[0.1, 0.6]);
set(gca,'XTickLabel',Y);
legend('Cohort','Duration','MT-Duration','Location','SouthWest');
title(SG(i));
end

figure;
plot(deltaSVD2,'LineWidth',1.3)
xlabel('Years');
ylabel('Difference of SVD metric');
set(gca, 'Xlim',[1, 12]);
set(gca,'XTick',[1:1:12]);
set(gca,'XTickLabel',Y);
legend('Cohort','Duration','ML','Location','NorthEast');





